package com.xl6667.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xl6667.constants.SystemConstants;
import com.xl6667.domain.ResponseResult;
import com.xl6667.domain.dto.PageDto;
import com.xl6667.domain.dto.TagDto;
import com.xl6667.domain.entity.Tag;
import com.xl6667.mapper.TagMapper;
import com.xl6667.service.TagService;
import com.xl6667.util.BeanCopyUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
* @author 熊磊
* @description 针对表【sg_tag(标签)】的数据库操作Service实现
* @createDate 2024-03-23 16:53:37
*/
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag>
    implements TagService{

    @Override
    public ResponseResult getTagList(Integer pageSize, Integer pageNum, String name, String remark) {
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        //备注
        queryWrapper.eq(StringUtils.hasText(remark),Tag::getRemark,remark);
        //标签名
        queryWrapper.like(StringUtils.hasText(name),Tag::getName,name);
        //没有删除
        queryWrapper.eq(Tag::getDelFlag, SystemConstants.DEL_FLAG_NORMA);

        Page<Tag> page = new Page<>(pageNum,pageSize);
        page(page,queryWrapper);
        List<Tag> records = page.getRecords();
        List<TagDto> tagDtos = BeanCopyUtil.copyBeanList(records, TagDto.class);
        PageDto pageDto = new PageDto(tagDtos,page.getTotal());
        return ResponseResult.okResult(pageDto);

    }

    @Override
    public ResponseResult getTag(Long id) {
        Tag tag = getById(id);
        TagDto tagDto = BeanCopyUtil.copyBean(tag,TagDto.class);
        return ResponseResult.okResult(tagDto);
    }

    @Override
    public ResponseResult addTag(Tag tag) {
        save(tag);
        return ResponseResult.okResult("添加成功");
    }

    @Override
    public ResponseResult delTag(List<Integer> id) {
        LambdaUpdateWrapper<Tag> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(Tag::getId,id).set(Tag::getDelFlag,1);
        update(null,updateWrapper);
        return ResponseResult.okResult("删除成功");
    }

    @Override
    public ResponseResult putTag(Tag tag) {
        updateById(tag);
        return ResponseResult.okResult("修改成功");
    }

    @Override
    public ResponseResult listAllTag() {
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Tag::getDelFlag,SystemConstants.DEL_FLAG_NORMA);
        List<Tag> tags = list(queryWrapper);
        List<TagDto> tagDtos = BeanCopyUtil.copyBeanList(tags, TagDto.class);
        return ResponseResult.okResult(tagDtos);
    }
}




